﻿using CommonType;
using MediatR;
using S_Gridding.Domains.FoundGridding;
using S_Gridding.Infrastructure.Interface.IRead;
using S_Gridding.Read.Api.Application.Commands;
using S_Userinfor.Domains;

namespace S_Gridding.Read.Api.Application.CommandsHanders
{
    public class CellgridShowCommandsHanders : IRequestHandler<CellgridShowCommands, PageResule<Cellgrid>>
    {
        private readonly IDCellgridRepository _dCellgrid;

        public CellgridShowCommandsHanders(IDCellgridRepository dCellgrid)
        {
            _dCellgrid = dCellgrid;
        }
        public async Task<PageResule<Cellgrid>> Handle(CellgridShowCommands request, CancellationToken cancellationToken)
        {
            var query = $" SELECT * FROM Cellgrid WHERE 1=1 AND IsDel !=TRUE ";

            if (!string.IsNullOrEmpty(request.Grid_name))
            {
                query += $" AND Grid_name LIKE '%{request.Grid_name}%' ";
            }
            var tacount = await _dCellgrid.SelectAsync(request.PageSize, query);

            query += $"  ORDER BY ID DESC  LIMIT {(request.PageIndex - 1) * request.PageSize},@PageSize;";

            var List = await _dCellgrid.SelectAsync(request.PageSize, query);

            return new PageResule<Cellgrid>
            {
                TotalCount = tacount.Count(),
                PageCount = (int)Math.Ceiling((List.Count() * 1.0) / request.PageSize),
                Data = List,
            };
        }
    }
}
